{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1c0cf975",
   "metadata": {},
   "source": [
    "# Jina"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "da922b13-eaa8-4cdc-98dd-cf8f3d2e6ffa",
   "metadata": {},
   "source": [
    "Install requirements"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2f0a1567-6273-47a3-b19d-c30af2470810",
   "metadata": {},
   "outputs": [],
   "source": [
    "pip install -U langchain-community"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7911b286-130d-4971-b77c-7c7a077115b6",
   "metadata": {},
   "source": [
    "Import libraries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d94c62b4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "from langchain_community.embeddings import JinaEmbeddings\n",
    "from numpy import dot\n",
    "from numpy.linalg import norm\n",
    "from PIL import Image"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "59aa1c02-1216-43eb-8473-8e0468f0ddb6",
   "metadata": {},
   "source": [
    "## Embed text and queries with Jina embedding models through JinaAI API"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "523a09e3",
   "metadata": {},
   "outputs": [],
   "source": [
    "text_embeddings = JinaEmbeddings(\n",
    "    jina_api_key=\"jina_*\", model_name=\"jina-embeddings-v2-base-en\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b212bd5a",
   "metadata": {},
   "outputs": [],
   "source": [
    "text = \"This is a test document.\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "57db66bd",
   "metadata": {},
   "outputs": [],
   "source": [
    "query_result = text_embeddings.embed_query(text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aea3ca33-1e6e-499c-8284-b7e26f38c514",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(query_result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b790fd09",
   "metadata": {},
   "outputs": [],
   "source": [
    "doc_result = text_embeddings.embed_documents([text])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c2e6b743-768c-4d7e-a331-27d5f0e8e30e",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(doc_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "338ea747-040e-4ed4-8ddf-9b2285987885",
   "metadata": {},
   "source": [
    "## Embed images and queries with Jina CLIP through JinaAI API"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "575b5867-59fb-4fd1-911b-afee2eaca088",
   "metadata": {},
   "outputs": [],
   "source": [
    "multimodal_embeddings = JinaEmbeddings(jina_api_key=\"jina_*\", model_name=\"jina-clip-v1\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a9b335f5-fa99-4931-95f6-7b187c0e2f30",
   "metadata": {},
   "outputs": [],
   "source": [
    "image = \"https://avatars.githubusercontent.com/u/126733545?v=4\"\n",
    "\n",
    "description = \"Logo of a parrot and a chain on green background\"\n",
    "\n",
    "im = Image.open(requests.get(image, stream=True).raw)\n",
    "print(\"Image:\")\n",
    "display(im)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ac2aace1-27af-4c4f-96f8-8e8b20d95b98",
   "metadata": {},
   "outputs": [],
   "source": [
    "image_result = multimodal_embeddings.embed_images([image])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6687808c-1977-4128-a960-888bb82c46e1",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(image_result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2844ef7c-cf9b-4e28-b627-09887aaa0a6d",
   "metadata": {},
   "outputs": [],
   "source": [
    "description_result = multimodal_embeddings.embed_documents([description])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "23372332-2ea3-4e4a-abc8-8307d45ebc95",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(description_result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "08d3ba5e-8957-4b10-97e3-40359ab165a6",
   "metadata": {},
   "outputs": [],
   "source": [
    "cosine_similarity = dot(image_result[0], description_result[0]) / (\n",
    "    norm(image_result[0]) * norm(description_result[0])\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6be56ff9-774b-4347-a5cf-57d8db9e2cf2",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(cosine_similarity)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
